/**
 * reduce函数可以根据需要进行累加，过滤，分组，映射等操作
 * reduce是数组方法，可以对数组中的每个元素一次执行一个回调函数，从左到右一次累计出一个最终的值
 * arr.reduce(callback(accumulator,currentValue[,index[,array]])[,initialValue])
 * accumulator:累积器，上次回调函数执行返回的值。
 * currentValue:当前元素的值。
 * index:当前元素的下标
 * array:元素数组
 * initialValue累积器初始值
 */

//计算数组中每个元素出现的次数
const fruits=['apple','banana','apple','orange','banana','banana'];
const count=fruits.reduce((accumulator,currentValue)=>{
    // console.log(accumulator,1112);
    accumulator[currentValue]=(accumulator[currentValue]||0)+1;
    return accumulator;
},{})
// console.log(count);

//拍平嵌套数组
const nestedArray=[[1,2],[3,4],[5,6],7]
//有个奇怪的问题，箭头函数不能包裹{}
const flattenedArray=nestedArray.reduce((accumulator,currentValue)=>{
    let a=accumulator.concat(currentValue)
    return a
},[])
// console.log(flattenedArray);

// 按条件分组--按年龄分组
const people=[
    {name:'Alice',age:25},
    {name:'Bob',age:26},
    {name:'Emily',age:26},
    {name:'Lucy',age:25},
    {name:'Amial',age:29},
    {name:'Tony',age:29}
]
const groupedPeople=people.reduce((accumulator,currentValue)=>{
    const key=currentValue.age;
    if(!accumulator[key]){
        accumulator[key]=[]
    }
    accumulator[key].push(currentValue);
    return accumulator;
},{})
// console.log(groupedPeople);

//将多个数组合并成为一个对象
const keys=['name','age','gender'];
const values=['Tong','25','female'];
const people1=keys.reduce((accumulator,currentValue,index)=>{
    accumulator[currentValue]=values[index];
    return accumulator;
},{})
// console.log(people1);

//将字符串转画成对象
const str='key1=value1&key2=value2&key3=value3&key4=value4'
const obj=str.split('&').reduce((accumulator,currentValue,index)=>{
    const [key,value]=currentValue.split('=');
    accumulator[key]=value;
    return accumulator;
},{})
console.log(obj);

//将对象转换为查询字符串
const params={foo:"bar",baz:42}
const queryStr=Object.entries(params).reduce((acc,[key,value])=>{
    return `${acc}${key}=${value}&`;
},"?").slice(0,-1);
console.log(queryStr);

//打印斐波那锲数列
const fibonacci=n=>{
    return [...Array(n)].reduce((accumulator,currentValue,index)=>{
        if(index<2){
            accumulator.push(index);
        }else{
            accumulator.push(accumulator[index-1]+accumulator[index-2]);
        }
        return accumulator;
    },[])
}
console.log(fibonacci(20));

//检查字符串是否是回文字符串
const str1='racecar';
const isPalindrome=str1.split(' ').reduce((accumulator,currentValue,index,array)=>{
    return accumulator&&currentValue===array[array.length-index-1];
},true)
console.log(isPalindrome);

//检查括号是否匹配
const str2="((()))(((((((())))))))"
const balanced=str2.split("").reduce((acc,cur)=>{
    if(cur==="("){
        acc++;
    }else if(cur===")"){
        acc--;
    }
    return acc;
},0)===0;
console.log(balanced);


//递归获取对象属性
const user={
    info:{
        name:"Jason",
        address:{
            home:"shanghai",
            company:"Xian"
        },
    },
};
function get(config,path,defaultVal){
    return path.split('.').reduce((config,name)=>config[name],config)||defaultVal;
    // return fallback;
}
console.log(get(user,"info.name"));
